﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using JPW.OnlinePaperSubmissionSystem.Project.Dto;
using Kendo.DynamicLinq;
using System.Data.Entity;
namespace JPW.OnlinePaperSubmissionSystem.Project
{
    
   // [AbpAuthorize(AppPermissions.Pages_Tenant_WriterInformation)]
    public class WriterInformationAppService : AbpZeroTemplateAppServiceBase, IWriterInformationAppService
    {
        private readonly IRepository<WriterInformation,long> _writerInformationRepository;
        public WriterInformationAppService(IRepository<WriterInformation,long> writerInformationRepository)
        {
            _writerInformationRepository = writerInformationRepository;
            
        }
        
        public DataSourceResult KendoGetWriterInformations(DataSourceRequest input)
        {
         var writerInformations = _writerInformationRepository
            .GetAll()
            .OrderBy(p => p.Id)
            .MapTo<List<WriterInformationListDto>>().AsQueryable()
            .ToDataSourceResult(input);

           return writerInformations;
        }
         public ListResultOutput<WriterInformationListDto> GetWriterInformations(GetWriterInformationInput input)
        {
            var writerInformations = _writerInformationRepository
            .GetAll()
            //.WhereIf(
            //    !input.Filter.IsNullOrEmpty(),
            //    p => p.Name.Contains(input.Filter) ||
            //            p.Code.Contains(input.Filter) 
                        //||p.Performance==Convert.ToInt32(input.Filter)
            //)
            .OrderBy(p => p.Id)
            .ToList();

            return new ListResultOutput<WriterInformationListDto>(writerInformations.MapTo<List<WriterInformationListDto>>());
        }
         public async Task<ListResultOutput<WriterInformationListDto>> GetWriterInformationsAsync()
        {
            var query = await _writerInformationRepository.GetAll().OrderBy(p => p.Id).ToListAsync();

            return new ListResultOutput<WriterInformationListDto>(query.MapTo<List<WriterInformationListDto>>());

        }
        
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_WriterInformation_Delete)]
        public async Task DeleteWriterInformation(IdInput input)
        {
            await _writerInformationRepository.DeleteAsync(input.Id);
        }
        
        public async Task CreateOrUpdateWriterInformation(CreateOrUpdateWriterInformationInput input)
        {
            if (input.WriterInformation.ID.HasValue)
            {
                await UpdateWriterInformationAsync(input);
            }
            else
            {
                await CreateWriterInformationAsync(input);
            }
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_WriterInformation_Create)]
        public async Task CreateWriterInformationAsync(CreateOrUpdateWriterInformationInput input)
        {
            var writerInformation = input.WriterInformation.MapTo<WriterInformation>();
            await _writerInformationRepository.InsertAsync(writerInformation);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_WriterInformation_Update)]
        public async Task UpdateWriterInformationAsync(CreateOrUpdateWriterInformationInput input)
        {
            var writerInformation = input.WriterInformation.MapTo<WriterInformation>();
            await _writerInformationRepository.UpdateAsync(writerInformation);
        }
      //  [AbpAuthorize(AppPermissions.Pages_Tenant_WriterInformation_Update)]
        public async Task<GetWriterInformationForEditOutput> GetWriterInformationForEdit(NullableIdInput input)
        {
            WriterInformation writerInformation = null;
            if (input.Id.HasValue) //Editing existing role?
            {
                writerInformation = await _writerInformationRepository.FirstOrDefaultAsync(input.Id.Value);
            }
            return new GetWriterInformationForEditOutput
            {
                WriterInformation = writerInformation.MapTo<WriterInformationEditDto>(),
            };
        }
        public async Task<GetWriterInformationForEditOutput> GetWriterInformationById(NullableIdInput input)
        {
            WriterInformation writerInformation = null;
            if (input.Id.HasValue) //Editing existing role?
            {
                writerInformation = await _writerInformationRepository.FirstOrDefaultAsync(x=>x.UserId==input.Id);
            }
            return new GetWriterInformationForEditOutput
            {
                WriterInformation = writerInformation.MapTo<WriterInformationEditDto>(),
            };
        }
    }
}

